package com.cwx.web;


import com.cwx.pojo.Tag;
import com.cwx.service.BlogService;
import com.cwx.service.TagsService;
import com.cwx.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller  //springboot里面不允许有两个同名的类
public class TagShowController {

    @Autowired
    public TagsService tagsService;

    @Autowired
    private BlogService blogService;

    //从导航去访问分类页面  携带一个参数-1  当前活跃的分类
    @GetMapping("/tags/{id}")
    public String types(@PageableDefault(size = 6,sort = "updateTime", direction = Sort.Direction.DESC) Pageable pageable
                        , @PathVariable Long id, Model model){
        //1、先查出所有分类  根据分类所对应博客的数量来查询所有的博客
        List<Tag> tags = tagsService.listTagTop(100000);
        model.addAttribute("tags",tags);

        //如果是从首页点到分类这来的时候，id的值就为-1,当前活跃度分类就是查出来所有分类的第一个
        if (id == -1){
             id = tags.get(0).getId();
        }
        //根据这个分类id查询这个博客
        model.addAttribute("page",blogService.listBlog(id,pageable));
        //然后为了显示那个为当前活跃的分类,把前台传过来的typeid传回去。
        model.addAttribute("activeTagId",id);
        model.addAttribute("newBlogs",blogService.listBlogTop(3));
        return "tags";
    }

}
